package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.DateUtil;
import com.vcc.data.entity.ShareAccountDetail;
import com.vcc.data.mapper.ShareAccountDetailMapper;
import com.vcc.service.client.share.detail.response.ClientShareAccountDetailResponse;
import com.vcc.service.service.ShareAccountDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

import static com.vcc.data.entity.table.CardTypeTableDef.CARD_TYPE;
import static com.vcc.data.entity.table.ShareAccountDetailTableDef.SHARE_ACCOUNT_DETAIL;
import static com.vcc.data.entity.table.ShareAccountTableDef.SHARE_ACCOUNT;
import static com.vcc.data.entity.table.UserCardTableDef.USER_CARD;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class ShareAccountDetailServiceImpl extends ServiceImpl<ShareAccountDetailMapper, ShareAccountDetail> implements ShareAccountDetailService {
    @Override
    public ShareAccountDetail create(Long shareAccountId, BigDecimal balance, String currencyCode, String channelCode, String type, String orderId, BigDecimal amount, String remark, Boolean hasTransferIn, Long cardTypeId, Long userCardId, Long userId, Long operationUserId) {
        ShareAccountDetail detail = new ShareAccountDetail();
        detail.setShareAccountId(shareAccountId);
        detail.setBalance(balance);
        detail.setCurrencyCode(currencyCode);
        detail.setChannelCode(channelCode);
        detail.setType(type);
        detail.setOrderId(orderId);
        detail.setAmount(amount);
        detail.setRemark(remark);
        detail.setHasTransferIn(hasTransferIn);
        detail.setCardTypeId(cardTypeId);
        detail.setUserCardId(userCardId);
        detail.setUserId(userId);
        detail.setOperationUserId(operationUserId);
        detail.setRequestDate(new Date());
        this.save(detail);
        return detail;
    }


    @Override
    public Page<ClientShareAccountDetailResponse> paging(Integer pageNumber, Integer pageSize,String orderId, Long shareAccountId, String type, String startDate, String endDate) {
        QueryChain<ShareAccountDetail> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT_DETAIL.ALL_COLUMNS);
        queryChain.select(SHARE_ACCOUNT.ACCOUNT_NAME.as("accountName"));
        queryChain.select(CARD_TYPE.SECTION_NO.as("sectionNo"));
        queryChain.select(USER_CARD.CARD_NUMBER.as("cardNumber"));
        queryChain.select(USER.EMAIL.as("account"));
        queryChain.from(SHARE_ACCOUNT_DETAIL);
        queryChain.leftJoin(SHARE_ACCOUNT).on(SHARE_ACCOUNT.ID.eq(SHARE_ACCOUNT_DETAIL.SHARE_ACCOUNT_ID));
        queryChain.leftJoin(USER_CARD).on(USER_CARD.ID.eq(SHARE_ACCOUNT_DETAIL.USER_CARD_ID));
        queryChain.leftJoin(CARD_TYPE).on(CARD_TYPE.ID.eq(SHARE_ACCOUNT_DETAIL.CARD_TYPE_ID));
        queryChain.leftJoin(USER).on(USER.ID.eq(SHARE_ACCOUNT_DETAIL.USER_ID));
        if (shareAccountId != null) {
            queryChain.and(SHARE_ACCOUNT_DETAIL.SHARE_ACCOUNT_ID.eq(shareAccountId));
        }
        if (StringUtils.isNotBlank(type)) {
            queryChain.and(SHARE_ACCOUNT_DETAIL.TYPE.eq(type));
        }
        if (StringUtils.isNotBlank(orderId)) {
            queryChain.and(SHARE_ACCOUNT_DETAIL.ORDER_ID.eq(orderId));
        }
        if (StringUtils.isNotBlank(startDate)) {
            queryChain.where(SHARE_ACCOUNT_DETAIL.REQUEST_DATE.ge(DateUtil.getDayBeginDate(startDate, "yyyy-MM-dd")));
        }
        if (StringUtils.isNotBlank(endDate)) {
            queryChain.where(SHARE_ACCOUNT_DETAIL.REQUEST_DATE.le(DateUtil.getDayEndDate(endDate, "yyyy-MM-dd")));
        }
        queryChain.orderBy(SHARE_ACCOUNT_DETAIL.REQUEST_DATE.desc());
        return queryChain.pageAs(
                Page.of(
                        pageNumber == null || pageNumber < 1 ? 1 : pageNumber,
                        pageSize == null || pageSize < 1 ? 20 : pageSize
                ),
                ClientShareAccountDetailResponse.class
        );
    }


    @Override
    public Boolean setCardNumberIdByOrderId(String orderId, Long userCardId) {
        return updateChain()
                .set(SHARE_ACCOUNT_DETAIL.USER_CARD_ID, userCardId)
                .where(SHARE_ACCOUNT_DETAIL.ORDER_ID.eq(orderId))
                .update();
    }
}
